package com.example.back.util;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * md5加密
 *
 * @author Administrator
 *
 */
public final class MD5Util {

	private final static char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	private static final String UTF_8 = "UTF-8";

	private static ThreadLocal<MessageDigest> threadLocal = new ThreadLocal<MessageDigest>() {
		@Override
		protected MessageDigest initialValue() {
			MessageDigest messagedigest = null;
			try {
				messagedigest = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			return messagedigest;
		}
	};

	private static String byte2HexStr(byte[] b) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; i++) {
			String s = Integer.toHexString(b[i] & 0xFF);
			if (s.length() == 1) {
				sb.append("0");
			}

			sb.append(s.toLowerCase());
		}

		return sb.toString();
	}
	public static String getStringMD5(String str, String charset) {
		byte[] buffer;
		try {
			buffer = str.getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		MessageDigest messagedigest = threadLocal.get();
		messagedigest.update(buffer);

		return byte2HexStr(messagedigest.digest());

	}
	public static String getStringMD5(String str) {
		byte[] buffer;
		try {
			buffer = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		MessageDigest messagedigest = threadLocal.get();
		messagedigest.update(buffer);

		return byte2HexStr(messagedigest.digest());

	}
	public final static String getFileMD5String(File file) {

//		FileInputStream in = null;
//		FileChannel ch = null;
//		try {
//			in = new FileInputStream(file);
//			ch = in.getChannel();
//			MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY,
//					0, file.length());
//			MessageDigest messagedigest = MessageDigest.getInstance("MD5");
//			messagedigest.update(byteBuffer);
//			return bufferToHex(messagedigest.digest());
//		} catch (Exception e) {
//			return null;
//		} finally {
//			try {
//				in.close();
//				ch.close();
//			} catch (Exception e) {
//			} finally {
//				in = null;
//				ch = null;
//			}
//		}

		try {
			return getMD5String(FileUtils.readFileToByteArray(file));
		} catch (IOException e) {
		}
		return null;
	}

	public final static String getMD5String(String s) {
		try {
			return getMD5String(s.getBytes(UTF_8));
		} catch (Exception e) {
			return getMD5String(s.getBytes());
		}
	}

	public final static String getMD5String(Object s) {
		return getMD5String(String.valueOf(s));
	}

	public final static String getMD5String(byte[] bytes) {
		try {
			MessageDigest messagedigest = MessageDigest.getInstance("MD5");
			messagedigest.update(bytes);
			return bufferToHex(messagedigest.digest());
		} catch (Exception e) {
			return null;
		}
	}

	private final static String bufferToHex(byte bytes[]) {
		return new String(encodeHex(bytes));
	}

	// >>>无符号，>>带符号

	private final static char[] encodeHex(byte[] data) {
		int l = data.length;
		char[] out = new char[l << 1];
		// two characters form the hex value.
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = HEX_DIGITS[(0xF0 & data[i]) >> 4];
			out[j++] = HEX_DIGITS[0x0F & data[i]];
		}
		return out;
	}
}
